package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.*;
//此类构建管理员对景点进行增删改查的界面
public class ManagerScenic extends JFrame {
	private JPanel managerScenicPanel;

	public ManagerScenic() {
		super("景点管理窗口");
		setSize(1000, 600);
		setLocation(200, 150);

		Font font = new Font("宋体", Font.PLAIN, 25);
		UIManager.put("OptionPane.font", font);
		UIManager.put("OptionPane.messageFont", font);
		UIManager.put("OptionPane.buttonFont", font);

		setVisible(true);
	}
	function.Scenic scenic = new function.Scenic();

	public void managerScenicPanel() {
		managerScenicPanel = new JPanel();
		managerScenicPanel.setLayout(null);

		ButtonGroup group0 = new ButtonGroup();
		/*将每个景点做成一个JRadioButton
		 * 组成一个ButtonGroup显示在界面左侧
		 * 管理员可通过选择不同景点而容易地对景点进行增删改查
		 */
		ArrayList<String> list = scenic.display();
		for (int i = 0; i < list.size(); i++) {
			JRadioButton rb = new JRadioButton(list.get(i), true);
			rb.setBounds(50, 50 + 30 * i, 300, 30);
			rb.setFont(new Font("宋体", Font.PLAIN, 25));
			managerScenicPanel.add(rb);
			group0.add(rb);
		}

		JLabel informationLabel = new JLabel("");
		informationLabel.setBounds(370, 240, 550, 150);
		informationLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(informationLabel);
		//不允许修改景点id，添加景点时景点id自动分配
		JLabel idLabel = new JLabel("id");
		idLabel.setBounds(400, 50, 70, 30);
		idLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(idLabel);

		JLabel idText = new JLabel("");
		idText.setBounds(450, 50, 100, 30);
		idText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(idText);

		JLabel nameLabel = new JLabel("景点名");
		nameLabel.setBounds(520, 50, 100, 30);
		nameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(nameLabel);

		JTextField nameText = new JTextField(20);
		nameText.setBounds(600, 50, 200, 30);
		nameText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(nameText);

		JLabel  introductionLabel = new JLabel("简介");
		introductionLabel.setBounds(380, 100, 80, 30);
		introductionLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(introductionLabel);

		JTextArea introductionText = new JTextArea(1, 3);
		introductionText.setBounds(470, 100, 400, 80);
		introductionText.setFont(new Font("宋体", Font.PLAIN, 25));
		introductionText.setLineWrap(true);
		managerScenicPanel.add(introductionText);

		JLabel popularityLabel = new JLabel("受欢迎度");
		popularityLabel.setBounds(360, 200, 150, 30);
		popularityLabel.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(popularityLabel);

		JTextField popularityText = new JTextField(5);
		popularityText.setBounds(470, 200, 100, 30);
		popularityText.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(popularityText);
		
		JButton buttonAdd = new JButton("添加");
		buttonAdd.setBounds(80, 450, 110, 40);
		buttonAdd.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(buttonAdd);

		buttonAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//自动为新添加的景点分配景点id，id号为景点数+1，即id号是连续的
				idText.setText(Integer.toString(scenic.getTotalNumber() + 1));
				scenic = new function.Scenic(idText.getText(), nameText.getText(), introductionText.getText(), popularityText.getText());
				//调用scenic类的add方法添加景点
				boolean a = scenic.add();
				//若添加成功（此时a==true）则同时将此景点加入按钮组在窗口中显示出来
				if (a) {
					ArrayList<String> list1 = scenic.display();
					/*新建一个JRadioButton，将新添加的景点
					 * 也加入初始的buttongroup中在页面左侧显示出来
					 */
					JRadioButton rb = new JRadioButton(idText.getText() + "  " + nameText.getText(), true);
					rb.setBounds(50, 50 + 30 * (list1.size()-1), 150, 30);
					rb.setFont(new Font("宋体", Font.PLAIN, 25));
					managerScenicPanel.add(rb);
					group0.add(rb);
					//添加成功后清除页面
					idText.setText("");
					nameText.setText("");
					introductionText.setText("");
					popularityText.setText("");
//					提示添加是否成功
					JOptionPane.showMessageDialog(null, "添加成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "添加失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JButton button2 = new JButton("删除");
		button2.setBounds(220, 450, 110, 40);
		button2.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(button2);
		button2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				AbstractButton btn;
				while (radioBtns.hasMoreElements()) {
					btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						group0.remove(btn);
						btn.setVisible(false);
						break;
					}
				}
				//调用scenic类的delete方法删除景点
				boolean a = scenic.delete(enable);
				if (a) {
					JOptionPane.showMessageDialog(null, "删除成功", "提示", JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "删除失败", "提示", JOptionPane.ERROR_MESSAGE);
				}

			}
		});

		/*在修改时，管理员选中要修改的景点，
		 * 先点击“修改”按钮，会将被选中的景点的内容显示出来
		 然后管理员可在已有内容上进行修改，
		 修改完毕后点击“保存修改”按钮即可将修改保存到scenic.xml中
		 	*/
		JButton button3 = new JButton("修改");
		button3.setBounds(370, 450, 100, 40);
		button3.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(button3);
		button3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}
//调用scenic类的find方法返回按钮选中的scenic对象
				scenic = scenic.find(enable);
				//将此景点的信息打印出来
				idText.setText(scenic.getId());
				nameText.setText(scenic.getName());
				introductionText.setText(scenic.getIntroduction());
				popularityText.setText(scenic.getPopularity());
			}
		});
		
		JButton buttonSaveModify = new JButton("保存修改");
		buttonSaveModify.setBounds(500, 450, 200, 40);
		buttonSaveModify.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(buttonSaveModify);
		buttonSaveModify.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}

				scenic = new function.Scenic(idText.getText(), nameText.getText(), introductionText.getText(),popularityText.getText());
//				调用scenic类的modify方法实现修改
				boolean a = scenic.modify(enable);
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns1 = group0.getElements();
				if (a) {
					while (radioBtns1.hasMoreElements()) {
						AbstractButton btn = radioBtns1.nextElement();
						if (btn.isSelected()) {
							btn.setText(idText.getText() + "  " + nameText.getText());
							break;
						}
					}
					//修改成功后清除界面
					idText.setText("");
					nameText.setText("");
					introductionText.setText("");
					popularityText.setText("");

					JOptionPane.showMessageDialog(null, "修改成功", "提示",JOptionPane.ERROR_MESSAGE);
				} else {
					JOptionPane.showMessageDialog(null, "修改失败", "提示", JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		JButton button = new JButton("查询");
		button.setBounds(715, 450, 110, 40);
		button.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(button);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String enable = "false";
				//获取buttongroup中选中按钮的文本
				Enumeration<AbstractButton> radioBtns = group0.getElements();
				while (radioBtns.hasMoreElements()) {
					AbstractButton btn = radioBtns.nextElement();
					if (btn.isSelected()) {
						enable = btn.getText();
						break;
					}
				}
				//调用scenic类的inquire方法实现查询
				String information = scenic.inquire(enable);
				informationLabel.setText(information);
			}
		});
		//点击离开按钮关闭当前窗口
		JButton button4 = new JButton("离开");
		button4.setBounds(840, 450, 100, 40);
		button4.setFont(new Font("宋体", Font.PLAIN, 25));
		managerScenicPanel.add(button4);
		button4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});

		managerScenicPanel.setVisible(true);
		add(managerScenicPanel);
		setVisible(true);
	}
}
